//https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.getLocation.html
var util = require('../../util/coordinate.js')
Page({
  data: {
    result_02: '',
    result_84: '',
    lng: '',
    lat: '',
    markers: []
  },
  onShow: function () {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      altitude: 'true', //获取高度
      isHighAccuracy: 'true', //开启高精度位置信息
      highAccuracyExpireTime: 3500, //高精度超时时间
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const altitude = res.altitude
        var point = util.transformFromWGSToGCJ(latitude, longitude)
        that.setData({
          result_84: longitude + "," + latitude + "," + altitude,
          result_02: point.longitude + "," + point.latitude + "," + altitude,
          lng: point.longitude,
          lat: point.latitude,
          markers: [{
            id: res.id,
            latitude: point.latitude,
            longitude: point.longitude,
          }],
        })
      }
    })
  },

  onHide: function () {
    var that = this
    that.setData({
      result_02: '',
      result_84: '',
    })
  },
  //点击小程序右上角即可转发分享本页面
  onShareAppMessage: function () {},

  //获取位置信息
  getLocation: function (e) {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      altitude: 'true', //获取高度
      isHighAccuracy: 'true', //开启高精度位置信息
      highAccuracyExpireTime: 3500, //高精度超时时间
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const altitude = res.altitude
        var point = util.transformFromWGSToGCJ(latitude, longitude)
        that.setData({
          result_84: longitude + "," + latitude + "," + altitude,
          result_02: point.longitude + "," + point.latitude + "," + altitude,
          lng: longitude,
          lat: latitude,
          markers: [{
            id: res.id,
            latitude: point.latitude,
            longitude: point.longitude,
          }],
        })
      }
    })
  },
  chooseLocation: function (e) {
    var that = this
    wx.chooseLocation({
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        var point = util.transformFromGCJToWGS(latitude, longitude)
        that.setData({
          result_02: longitude + "," + latitude,
          result_84: point.longitude + "," + point.latitude,
          lng: longitude,
          lat: latitude,
          markers: [{
            id: res.id,
            latitude: latitude,
            longitude: longitude,
          }],
        })
      }
    })

  },


  copy_02: function (e) {
    if (e.currentTarget.dataset.text.length > 0) {
      var that = this
      that.setData({
        result_02: ''
      })
      wx.setClipboardData({
        data: e.currentTarget.dataset.text
      })
    }
  },
  copy_84: function (e) {
    if (e.currentTarget.dataset.text.length > 0) {
      var that = this
      that.setData({
        result_84: ''
      })
      wx.setClipboardData({
        data: e.currentTarget.dataset.text
      })
    }
  }

})